﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Threading;
using System.Windows.Threading;

namespace nLearning.UC
{
    /// <summary>
    /// Interaction logic for VDOControl.xaml
    /// </summary>
    public partial class VDOControl : UserControl
    {
        Border bdr;
        bool isMouseCaptured = false;
        public VDOControl()
        {
            InitializeComponent();

            linearGauge.MouseLeftButtonDown += (s1, e1) =>
            {
                bdr = e1.OriginalSource as Border;
                isMouseCaptured = true;
                bdr.Cursor = Cursors.Hand;
                bdr.CaptureMouse();
            };
            linearGauge.MouseMove += (s3, e3) =>
            {
                if (isMouseCaptured)
                {
                    var pathlength = linearGauge.ActualWidth * linearGauge.XAxisLength;
                    var startposition = linearGauge.ActualWidth * linearGauge.XAxisLocation;
                    var offset = Convert.ToDouble((linearGauge.Maximum - linearGauge.Minimum) / pathlength);

                    var pixelvalue = Convert.ToDouble(offset * startposition);

                    linearGauge.Value = Convert.ToDouble((e3.GetPosition(linearGauge).X * offset) - pixelvalue + linearGauge.Minimum);
                    if (linearGauge.Value < linearGauge.Minimum)
                        linearGauge.Value = linearGauge.Minimum;
                    else if (linearGauge.Value > linearGauge.Maximum)
                        linearGauge.Value = linearGauge.Maximum;
                }
            };
            linearGauge.MouseLeftButtonUp += (s2, e2) =>
            {
                if (bdr != null)
                {
                    Console.WriteLine(linearGauge.Value);
                    mediaElement1.Position = new TimeSpan(0, 0, 0, 0, (int)linearGauge.Value);
                    bdr.ReleaseMouseCapture();
                    bdr.Cursor = Cursors.Arrow;
                    isMouseCaptured = false;
                }
            };
        }
        Warehouse.LECTURE_INFO tagInfo;
        private string _fileURI;

        public string FileURI
        {
            get { return _fileURI; }
            set { _fileURI = value; }
        }

        public Warehouse.LECTURE_INFO TagInfo
        {
            get { return tagInfo; }
            set { tagInfo = value; }
        }
        private void UpdateVDOTime()
        {
            while (controlBar.Visibility == System.Windows.Visibility.Visible)
            {
                this.linearGauge.Dispatcher.Invoke(DispatcherPriority.Normal, new Action(() => { linearGauge.Value = mediaElement1.Position.TotalMilliseconds; }));
                Thread.Sleep(1000);
            }
        }

        bool IsPlaying()
        {
            var pos1 = mediaElement1.Position;
            System.Threading.Thread.Sleep(30);
            var pos2 = mediaElement1.Position;

            return (pos2 - pos1).TotalMilliseconds > 20;
        }

        private void ucVDOPlay_Loaded(object sender, RoutedEventArgs e)
        {
            //C:\Users\Angolous\Downloads\Sean Paul - She Doesn't Mind [Official Music Video].mp4
            //mediaElement1.Source = new Uri(@"C:\Users\Angolous\Downloads\Sean Paul - She Doesn't Mind [Official Music Video].mp4");
            //mediaElement1.Source = new Uri(@"C:\Users\Friends\Downloads\ploy.flv");
            mediaElement1.Source = new Uri(FileURI);
            Console.WriteLine(FileURI);
            mediaElement1.Play();
            TimeSpan ts = new TimeSpan(0, 0, 0, 0, 0);
            mediaElement1.Position = ts;
        }

        private void mediaElement1_MediaOpened(object sender, RoutedEventArgs e)
        {
            linearGauge.Maximum = mediaElement1.NaturalDuration.TimeSpan.TotalMilliseconds;
            //int slider = (int)trackBarEdit1.Value;
            //TimeSpan ts = new TimeSpan(0, 0, 0, 0, slider);
            //mediaElement1.Position = ts;
        }

        Thread thSetBar; //= new Thread(new ThreadStart(UpdateVDOTime));
        private void controlBar_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            thSetBar = new Thread(new ThreadStart(UpdateVDOTime));
            thSetBar.Name = "SetPosition";
            thSetBar.IsBackground = true;
            if (controlBar.Visibility == System.Windows.Visibility.Visible)
            {
                thSetBar.Start();
            }
            else
            {
                thSetBar.Abort();
            }
        }

        private void mediaElement1_PreviewMouseUp(object sender, MouseButtonEventArgs e)
        {
            if (controlBar.Visibility == System.Windows.Visibility.Visible)
            {
                controlBar.Visibility = System.Windows.Visibility.Hidden;
            }
            else
                controlBar.Visibility = System.Windows.Visibility.Visible;
        }

        private void back_Click(object sender, MouseButtonEventArgs e)
        {
            mediaElement1.Stop();
            if (thSetBar.IsAlive)
            {
                thSetBar.Abort();
            }
            UC.Lecture lecture = new Lecture();
            lecture.LectureInfo = this.TagInfo;
            WpfPageTransitions.PageTransition transition = new WpfPageTransitions.PageTransition();
            transition.TransitionType = WpfPageTransitions.PageTransitionType.Slide;
            transition.ShowPage(lecture);
            Window.GetWindow(this).Content = transition;
        }

        private void image1_PreviewMouseUp(object sender, MouseButtonEventArgs e)
        {
            BitmapImage play = new BitmapImage();
            play.BeginInit();
            play.UriSource = new Uri("pack://application:,,,/nLearning;component/Images/Player/black-play/play-3-64.png");
            play.EndInit();

            BitmapImage pause = new BitmapImage();
            pause.BeginInit();
            pause.UriSource = new Uri("pack://application:,,,/nLearning;component/Images/Player/black-pause/pause-3-64.png");
            pause.EndInit();

            if (IsPlaying())
            {
                mediaElement1.Pause();
                imgPlay.Source = play;
            }
            else
            {
                mediaElement1.Play();
                imgPlay.Source = pause;
            }
        }

        private void image3_PreviewMouseUp(object sender, MouseButtonEventArgs e)
        {
            if (trackBarVol.Value < 100)
                trackBarVol.Value += 5;
        }

        private void image2_PreviewMouseUp_1(object sender, MouseButtonEventArgs e)
        {
            if (trackBarVol.Value > 0)
                trackBarVol.Value -= 5;
        }

        private void trackBarVol_EditValueChanged(object sender, DevExpress.Xpf.Editors.EditValueChangedEventArgs e)
        {
            mediaElement1.Volume = (double)(trackBarVol.Value / 100.0);
        }

        private void imgPrevious_PreviewMouseUp(object sender, MouseButtonEventArgs e)
        {
            TimeSpan curTime = mediaElement1.Position;
            TimeSpan ts = TimeSpan.FromSeconds(-10);
            mediaElement1.Position = curTime.Add(ts);

        }
    }
}
